package com.sx.micowell.module.mkVideoView;

import android.util.Log;
import android.widget.ImageView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.bumptech.glide.Glide;
import com.dou361.ijkplayer.listener.OnShowThumbnailListener;
import com.dou361.ijkplayer.widget.PlayStateParams;
import com.dou361.ijkplayer.widget.PlayerView;
import com.example.mvvm.base.data.DataBindingConfig;
import com.gyf.immersionbar.ImmersionBar;
import com.sx.micowell.R;
import com.sx.micowell.databinding.ActMkVideoBinding;
import com.sx.micowell.framework.app.IntentKey;
import com.sx.micowell.framework.app.MyActivity;
import com.sx.micowell.framework.app.MyApplication;

import static com.sx.micowell.framework.arouth.RoutePath.MK_VIDEO_VIEW;

/**
 * @Description
 * @Author 梁浩文
 * @Date 2021/9/24 16:03
 */
@Route(path = MK_VIDEO_VIEW)
public class MkVideoVideoActivity extends MyActivity<ActMkVideoBinding> {
    public PlayerView player;
    @Autowired
    public String title;
    @Autowired(name = IntentKey.MP4)
    public String mp4;
    @Autowired(name = IntentKey.URL)
    public String url;

    @Override
    public void initView() {
//        ImmersionBar.with(this).titleBar(findViewById(R.id.app_video_top_box)).statusBarDarkFont(true).init();
    }

    @Override
    public void initData() {
        initVideoPlayer();
        startVideo(mp4,url);
    }

    @Override
    protected DataBindingConfig getDataBindingConfig() {
        return new DataBindingConfig(R.layout.act_mk_video);
    }

    /**
     * 初始化视频播放器
     */
    private void initVideoPlayer() {
        player = new PlayerView(this)
                .setScaleType(PlayStateParams.fitparent)
                .setOnlyFullScreen(true)
                .startPlay();
    }

    public void startVideo(String mp4, String image) {
        player.showThumbnail(new OnShowThumbnailListener() {
            @Override
            public void onShowThumbnail(ImageView ivThumbnail) {
                Glide.with(MkVideoVideoActivity.this)
                        .load(image)
                        .placeholder(R.drawable.bg)
                        .error(R.drawable.bg)
                        .into(ivThumbnail);
            }
        });
        player.setPlaySource(
                MyApplication.Companion.instance().getProxy(MkVideoVideoActivity.this).getProxyUrl(mp4)
        ).startPlay();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (player != null) {
            player.pausePlay();
            player.onDestroy();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (player != null) {
            player.pausePlay();
        }
    }
}
